import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/http/http_manager.dart';
import 'package:flutter_app/model/tree_resp.dart';
import 'package:flutter_app/provider/common_provider.dart';
import 'package:flutter_app/res/colours.dart';
import 'package:provider/provider.dart';

import '../constant.dart';

//第一种自定义回调方法
typedef MyCallBackFuture<T> = void Function(T);

class TreePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _TreePage();
}

class _TreePage extends State<TreePage> with AutomaticKeepAliveClientMixin {
  CommonProvider<TreeGroup> _naviProvider = CommonProvider<TreeGroup>();

  @override
  void initState() {
    super.initState();
    _requestData();
  }

  void _requestData() {
    var httpManager = HttpManager();
    var async = httpManager.getAsync(
      url: Constant.API_TREE,
      tag: Constant.API_TREE,
    );
    async.then((value) {
      var list = value.map<TreeGroup>((json) => TreeGroup.fromJson(json)).toList();
      _naviProvider.setDataList(list);
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<CommonProvider>(
      create: (_) => _naviProvider,
      child: Container(
          color: Colors.white,
          child: Consumer<CommonProvider>(
            builder: (context, provider, widget) => ListView.builder(
                itemCount: _naviProvider.list.length,
                shrinkWrap: true,
                itemBuilder: (context, index) {
                  return Column(
                    children: [
                      Padding(
                        padding: EdgeInsets.only(top: 10),
                        child: Text(
                          _naviProvider.list[index].name,
                          style: TextStyle(
                              fontSize: 18,
                              color: Colours.ff2c2c2c,
                              fontWeight: FontWeight.w500),
                        ),
                      ),
                      _buildWrapItem(_naviProvider.list[index].children)
                    ],
                  );
                }),
          )),
    );
  }

  Widget _buildWrapItem(List<Children> list) {
    return Padding(
      padding: EdgeInsets.all(10),
      child: Wrap(
        spacing: 10,
        runSpacing: 10,
        children: List.generate(
            list.length,
            (index) => Container(
                  padding:
                      EdgeInsets.only(left: 10, right: 10, top: 2, bottom: 2),
                  decoration: BoxDecoration(
                      color: Colours.fff1f2f4,
                      borderRadius: BorderRadius.all(Radius.circular(20))),
                  child: Text(
                    list[index].name,
                    style: TextStyle(fontSize: 16, color: Colours.ff2c2c2c),
                  ),
                )),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
